package com.example.springboot_log.Test;


import com.qiniu.util.Auth;
import com.qiniu.util.Base64;
import com.qiniu.util.StringMap;
import com.qiniu.util.UrlSafeBase64;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import java.io.File;
import java.io.FileInputStream;

/**
 * 七牛云
 */
public class put64 {
    String ak = "4Ar3NTGHPj5rBl_5Iv1kpbsX9EG-J-1Rgsdk9HUm";
    String sk = "_8fHnfv0if5RMhlrGtGuefMx7O2W5QmpQWmGaxj9";    // 密钥配置
    Auth auth = Auth.create(ak, sk);    // TODO Auto-generated constructor stub
    String bucketname = "summer0603";    //空间名
    String key = "2.jpg";    //上傳到七牛雲存儲時命名的图片名

    public String getUpToken() {
        return auth.uploadToken(bucketname, null, 3600, new StringMap().put("insertOnly", 1));
    }

    public void put64image() throws Exception {
        String file = "D:\\restaurantRes\\1622613114887febba7.jpg";//图片路径
        //这里改成你的图片存储的路径
        FileInputStream fis = null;
        int l = (int) (new File(file).length());
        byte[] src = new byte[l];
        fis = new FileInputStream(new File(file));
        fis.read(src);
        String file64 = Base64.encodeToString(src, 0);
        String url = "http://upload-z2.qiniup.com/putb64/" + l + "/key/" + UrlSafeBase64.encodeToString(key);
        //http://up-z1.qiniup.com是华北地区的域名链接
        RequestBody rb = RequestBody.create(null, file64);
        Request request = new Request.Builder().
                url(url).
                addHeader("Content-Type", "application/octet-stream")
                .addHeader("Authorization", "UpToken " + getUpToken())
                .post(rb).build();
        System.out.println(request.headers());
        OkHttpClient client = new OkHttpClient();
        Response response = client.newCall(request).execute();
        System.out.println(response);
    }

    public static void main(String[] args) throws Exception {
        new put64().put64image();
    }
}


